mui.init();
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var mask = mui.createMask(); //遮罩层
var pwds;
window.onload = function() {
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.plusReady(function() {
//		plus.nativeUI.showWaiting("加载中...");
	});
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();		
	});
}
mui('body').on('tap', '.mui-control-item', function() {
//	window.top.location.href = this.href;
	$(this).addClass("mui-active").siblings().removeClass('mui-active');
	var statusText = $(this).attr('data-status');
	console.log(statusText)
	getData(statusText)
	
});
var mask = mui.createMask(); //遮罩层
//点击我的页面
document.getElementById("back").addEventListener("tap", function() {
	window.top.location.href = 'my_user.html'; //返回我的
});

mui.back = function() {
	window.top.location.href = 'my_user.html'; //返回我的
	/*mui.openWindow({
		url: "my_user.html"
	});*/
}
var pay_password; //支付密码
var is_realname; //实名认证
mui.init({
	pullRefresh: {
		container: "#refreshContainer", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		up: {
			height: 50, // 可选.默认50.触发上拉加载拖动距离
			auto: true, // 可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", // 可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多的数据了', // 可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: pullupRefresh //加载数据方法 
		}
	}
})
//mui.ready(function() {
//	mui('.mui-scroll-wrapper').scroll({
//		bounce: true,
//		indicators: true,
//		deceleration: mui.os.ios ? 0.003 : 0.0009
//	});
//});

//代售、自用  self_btns  sello_btns
$(document).on("tap", ".self_btns", function() {
	id = $(this).attr("uid");
	console.log(id)
	//自用
		var dataOrder = {
			order_goods_id:id,
		};
		// console.log(JSON.stringify(dataOrder));
		var btnArray = ['确定', '取消'];
		mui.confirm('确定要此操作吗？', '温馨提示', btnArray, function(e) {
            if (e.index == 1) {
            	//取消
                // console.log('取消');
            }
            else {
            	//确定
                // console.log('确定');
                plus.nativeUI.showWaiting("加载中...");
				mask.show(); //显示遮罩层
				mui.ajax(goods_url + '/api/Replace/oneselfUse', {
					data:dataOrder,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						console.log(JSON.stringify(data))
						plus.nativeUI.closeWaiting(); //关闭等待
						mask.close(); //关闭遮罩层
						if(data.code == 10000) {
							mui.toast("自用，提交成功！");
							window.top.location.href = "../my/purchase_order.html";
						}
						else{
							mui.alert(data.msg);
						}
					},
					error: function(xhr, type, errorThrown) { //异常处理；
						console.log(type);
						plus.nativeUI.closeWaiting(); //关闭等待
						mask.close(); //关闭遮罩层
					}
				});
            }
       })
})
$(document).on("tap", ".sello_btns", function() {
	id = $(this).attr("uid");
	console.log(id)
	//代售
		var dataOrder = {
			order_goods_id:id,
		};
		// console.log(JSON.stringify(dataOrder));
		var btnArray = ['确定', '取消'];
		mui.confirm('确定要此操作吗？', '温馨提示', btnArray, function(e) {
            if (e.index == 1) {
            	//取消
                // console.log('取消');
            }
            else {
            	//确定
                // console.log('确定');
                plus.nativeUI.showWaiting("加载中...");
				mask.show(); //显示遮罩层
				mui.ajax(goods_url + '/api/Replace/replaceSell', {
					data:dataOrder,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						console.log(JSON.stringify(data))
						plus.nativeUI.closeWaiting(); //关闭等待
						mask.close(); //关闭遮罩层
						if(data.code == 10000) {
							mui.toast("代售，提交成功！");
							window.top.location.href = "../my/purchase_order.html";
						}
						else{
							mui.alert(data.msg);
						}
					},
					error: function(xhr, type, errorThrown) { //异常处理；
						console.log(type);
						plus.nativeUI.closeWaiting(); //关闭等待
						mask.close(); //关闭遮罩层
					}
				});
            }
       })
})
//代售、自用  self_btns  sello_btns
$(document).on("tap", ".sureReceipt", function() {
	id = $(this).attr("uid");
	console.log(id)
	//自用
		var dataOrder = {
			order_goods_id:id,
		};
		// console.log(JSON.stringify(dataOrder));
		var btnArray = ['确定', '取消'];
		mui.confirm('确定要此操作吗？', '温馨提示', btnArray, function(e) {
            if (e.index == 1) {
            	//取消
                // console.log('取消');
            }
            else {
            	//确定
                // console.log('确定');
                plus.nativeUI.showWaiting("加载中...");
				mask.show(); //显示遮罩层
				mui.ajax(goods_url + '/api/user/receiving', {
					data:dataOrder,
					dataType: 'json', //服务器返回json格式数据
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						console.log(JSON.stringify(data))
						if(data.code == 10000){ 
							mui.toast("已确认收货 ");
							window.top.location.href = "../my/purchase_order.html";
						}else {
							mui.toast(data.msg);
						}
					},
					error: function(xhr, type, errorThrown) { //异常处理；
						console.log(type);
						plus.nativeUI.closeWaiting(); //关闭等待
						mask.close(); //关闭遮罩层
					}
				});
            }
       })
})


//获取本地信息
//function daishou(type,id){
//	if(type==1){
//		
//	}
//	else if(type==2){
//		
//	}
//}
var user_id = localStorage.getItem("user_id");
var pagenum = 1; //页数
var pagesize = 20; //每页条数
var isOver = false; //是否加载完  
function getData(order_status) {
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.ajax(goods_url + '/api/Member/index', {
		data:{
			page:pagenum,
			order_status: order_status
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(datas) {				
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			console.log('订单列表数据');
			console.log(JSON.stringify(datas))
			var p = datas.resp.total / pagesize; //总页数
			if(pagenum >= p) { //当前的页数
					isOver = true;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
				}
			if(isOver == false) {
					pagenum++;
					mui('#refreshContainer').pullRefresh().endPullupToRefresh(true); //停止正在加载
					mui('#refreshContainer').pullRefresh().enablePullupToRefresh(); //显示上拉加载文字
				}
			var html= "";
			for(var i = 0;i<datas.resp.data.length;i++){				
				html += '<ul class="mui-table-view orderCon">';				
				html += '<li class="mui-table-view-cell">'
				html += '<div class="type-ord-bar">'
				html += '<i>'+datas.resp.data[i].shop_name+'</i>'
				if(datas.resp.data[i].replace_type == 4){
					html += '<u>代售中</u>'
				}
				else if(datas.resp.data[i].replace_type == 5){
					html += '<u>代售完成</u>'
				}
				else{
					html += '<u>' + datas.resp.data[i].order_status + '</u>'
				}
				html += '</div>'
				html += '</li>';
				var goodsOrder = datas.resp.data[i].order_goods;
				for(var  z = 0; z< goodsOrder.length;z++){
					html += '<li class="mui-table-view-cell mui-media type-ord-imginfo">'
					if(goodsOrder[z].album_picture!=null){
						html += '<img class="mui-media-object mui-pull-left img" src="' + urlPic+goodsOrder[z].album_picture.pic_size_micro+ '">'
					}else {
						html += '<img class="mui-media-object mui-pull-left img" src="../../images/banner_1.jpg">'
					}					
					html += '<div class="mui-media-body">'
					html += '<div class="goodsName">' + goodsOrder[z].goods_name + '</div>'
					html += '<div class="goodsMoney">'
					html += '<span class="mui-pull-left goods_left">￥<span class="goods_left">' + goodsOrder[z].price+ '</span></span>'
					html += '<span class="mui-pull-right goods_right">x<span class="goods_right">' +goodsOrder[z].num+ '</span></span>'
					html += '</div>'
					html += '<p class="ord-size">'
					html += '<i>' +goodsOrder[z].sku_name+ '</i>'
					html += '</p>'
					html += '</div>'					
					html += '</li>';						
					if(datas.resp.data[i].order_status!='待付款'){
						if(goodsOrder[z].is_silverwork == 1 && goodsOrder[z].replace_type == 3 && goodsOrder[z].shop_id == 0){
							html += '<li class="mui-table-view-cell orderli type-ord-btnbar daishou-bar">'
							html += '<div class="btnons goPayment self_btns" uid=' + goodsOrder[z].order_goods_id + '>自用</div>'
							html += '<div class="btnons goPayment forBtnTex sello_btns" uid=' + goodsOrder[z].order_goods_id + '>代售</div>'
							html += '</li>';
						}else if(goodsOrder[z].replace_type == 1 && goodsOrder[z].goods_order_status== 2){
							html += '<li class="mui-table-view-cell orderli type-ord-btnbar daishou-bar">'
							html += '<div class="btnons goPayment forBtnTex sureReceipt" uid=' + goodsOrder[z].order_goods_id + '>确认收货</div>'
							html += '</li>';
						}
//						else if(goodsOrder[z].is_silverwork == 1 && goodsOrder[z].replace_type == 4 && goodsOrder[z].shop_id == 0){
//							html += '<li class="mui-table-view-cell orderli type-ord-btnbar daishou-bar">'
//							html += '<p class="oneself_icon">代售中</p>'
//							html += '</li>';
//						}
//						else if(goodsOrder[z].is_silverwork == 1 && goodsOrder[z].replace_type == 5 && goodsOrder[z].shop_id == 0){
//							html += '<li class="mui-table-view-cell orderli type-ord-btnbar daishou-bar">'
//							html += '<p class="oneself_icon">代售完成</p>'
//							html += '</li>';
//						}
					}
					if(goodsOrder[z].is_silverwork == 0 && goodsOrder[z].goods_order_status == 2){
						html += '<li class="mui-table-view-cell orderli type-ord-btnbar daishou-bar">'
						html += '<div class="btnons goPayment forBtnTex sureReceipt" uid=' + goodsOrder[z].order_goods_id + '">确认收货</div>'
						html += '</li>'
				
					} 
				}
				html += '<li class="mui-table-view-cell mui-media type-ord-imginfo total-moneybar">'
				html += '<div class="ord-tall-info">共计<i>' +datas.resp.data[i].total_num+ '</i>件商品  合计：￥<span>' + datas.resp.data[i].total_money + '</span></div>'
				html += '</li>'				
				if(datas.resp.data[i].order_status=='待付款'){
					html += '<li class="mui-table-view-cell orderli type-ord-btnbar">'
					html += '<div class="btnons goPayment goPayments" uider = ' + datas.resp.data[i].total_money + ' uid=' + datas.resp.data[i].out_trade_no + '>去付款</div>'
					html += '</li>';
				}
				html += '</ul>';				
			}			
			$("#my_list").html(html);
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
}
//确认收货
$(document).on("tap", ".confirm_the_goods", function() {
	$(".bg-model").fadeTo(300, 1)　　 //隐藏窗体的滚动条
	　　 $("body").css({
		"overflow": "hidden"
	});
	id = $(this).attr("uid");
})
$(document).on("tap", "#isok", function() {
	$(".bg-model").hide();　　 //显示窗体的滚动条
	$(".bg-models").hide();
	$("body").css({
		"overflow": "visible"
	});
	plus.nativeUI.showWaiting("加载中...");
	mask.show(); //显示遮罩层
	mui.ajax(order_url + '/order/orderToSignFor', {
		data: {
			"id": id,
			"sign": "string"
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			//console.log(JSON.stringify(data));
			if(data.code == 10000) {
				plus.nativeUI.closeWaiting(); //关闭等待
				mask.close(); //关闭遮罩层
				mui.alert("收货成功！", function() {
					window.location.reload(); //页面实现刷新(和跳转)
				});
			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) {
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			//异常处理；
			console.log(type);
		}
	})
});
//去付款
$(document).on("tap", ".goPayments", function() {
	$(".bg-models").fadeTo(300, 1)　　 //隐藏窗体的滚动条
	　　 $("body").css({
		"overflow": "hidden"
	});
	id = $(this).attr("uid");
	console.log(id)
	uider = $(this).attr("uider");
	$("#pay_price").text(Number(uider).toFixed(2));
})
$(document).on("tap", "#isoks", function() {
	$(".bg-model").hide();　　 //显示窗体的滚动条
	$(".bg-models").hide();
	$("body").css({
		"overflow": "visible"
	});
	payPanel.showPanel(function() {
		payPanel.closePanel();
		judge_payment_password();
		//payPanel.isShow;该属性能够判断面板是否关闭，可用于点击返回键时判断如果面板没关闭就关闭面板，而不是返回上个页面
	});
})
$(".bg-model-close").click(function() {　　
	$(".bg-model").hide();　　 //显示窗体的滚动条\
	$(".bg-models").hide();
	$(".bg-modelss").hide();
	$("body").css({
		"overflow": "visible"
	});
})

function pullupRefresh() {
	setTimeout(function() {
		mui('#refreshContainer').pullRefresh().disablePullupToRefresh(false);
		setTimeout(function() {
			getData();
			mui('#refreshContainer').pullRefresh().endPullupToRefresh((isOver)); //参数为true代表没有更多数据了。
		}, 500);
	}, 500);
}
//判断ios
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
if(isiOS) {
	$("#my_list").css("margin-top", "86px")
}

function orderPay() {
	var dd = {
			out_trade_no:id,
			pay_type:3
		}
	mui.ajax(goods_url + '/api/Pay/payment', {
		data: dd,
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			console.log(JSON.stringify(data));
			if(data.success == true){
				plus.nativeUI.closeWaiting(); //关闭等待
				mask.close(); //关闭遮罩层
				payPanel.closePanel();
				judge_payment_password()
				mui.alert("付款成功！", function() {
					window.location.reload(); //页面实现刷新(和跳转)
				});
				
			}else if(data.code == -15){
				plus.nativeUI.closeWaiting(); //关闭等待
				mask.close(); //关闭遮罩层
				mui.alert(data.msg);
			}
//			if(data.resp == 10028) {
//				plus.nativeUI.closeWaiting(); //关闭等待
//				mask.close(); //关闭遮罩层
//				mui.alert("余额不足！");
//			} else {
//				plus.nativeUI.closeWaiting(); //关闭等待
//				mask.close(); //关闭遮罩层
//				mui.alert("付款成功！", function() {
//					window.location.reload(); //页面实现刷新(和跳转)
//				});
//			}
		},
		error: function(xhr, type, errorThrown) {
			plus.nativeUI.closeWaiting(); //关闭等待
			mask.close(); //关闭遮罩层
			//异常处理；
			console.log(type);
		}
	})
}


function judge_payment_password() {
	var cc = {
			"password":pwds, //支付密码
			"out_trade_no":id,
			"pay_type":3
		}
	console.log(JSON.stringify(cc))
	mui.ajax(goods_url + '/api/Pay/confirmPws', { //确认支付（判断支付密码是否正确）接口
		data:cc,
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Content-Type': 'application/json'
		},
		success: function(data) {
			console.log(JSON.stringify(data))
			if(data.code == 1000) {
				mui.alert(data.msg)
				window.location.reload(); //页面实现刷新(和跳转)
			 }else{
				mui.alert(data.msg)
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
			var error_msg = JSON.parse(xhr.responseText)
			mui.alert(error_msg.msg)
		}
	});
}
//取消订单
//$(document).on("tap", ".close_order", function() {
//	$(".bg-modelss").fadeTo(300, 1)　　 //隐藏窗体的滚动条
//	　　 $("body").css({
//		"overflow": "hidden"
//	});
//})
//物流
//$(document).on("tap", ".see_logistics", function() {
//	mui.openWindow({
//		url: "logistics.html",
//		id: 'logistics'
//	});
//})
////退货退款
//$(document).on("tap", ".return_goods", function() {
//	mui.openWindow({
//		url: "refund_application.html",
//		id: 'refund_application',
//		createNew: true
//	});
//})
//----------------------------------------------------支付输入密码界面效果代码开始------------------------------------------------------